package com.hanson.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanson.mapper.TbIpVisitLogMapper;
import com.hanson.pojo.vo.TbIpVisitLog;
import com.hanson.service.ITbIpVisitLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * ip访问次数统计记录表 服务实现类
 * </p>
 *
 * @author HansonLee
 * @since 2022-08-31
 */

@Slf4j
@Service
public class TbIpVisitLogServiceImpl extends ServiceImpl<TbIpVisitLogMapper, TbIpVisitLog> implements ITbIpVisitLogService {

    @Resource
    private TbIpVisitLogMapper tbIpVisitLogMapper;

    /**
     * 分页查询tbIpVisitLog
     *
     * @param condition 数据筛选条件
     * @param page      分页对象
     * @return {@link Page< TbIpVisitLog>}      传进来的分页对象，封装了查询数据结果集
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public Page<TbIpVisitLog> list(String condition, Page<TbIpVisitLog> page) {
        QueryWrapper<TbIpVisitLog> wrapper = new QueryWrapper<TbIpVisitLog>();
        TbIpVisitLog tbIpVisitLog = new TbIpVisitLog();
        //↓↓↓↓↓↓↓↓为TbIpVisitLog查询对象设置条件set condition↓↓↓↓↓↓↓↓

        //↑↑↑↑↑↑↑↑为TbIpVisitLog查询对象设置条件set condition↑↑↑↑↑↑↑↑
        wrapper.setEntity(tbIpVisitLog);
        return super.page(page, wrapper);
    }

    /**
     * 添加新tbIpVisitLog
     *
     * @param tbIpVisitLog 需要添加的tbIpVisitLog
     * @return {@link int}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public int add(TbIpVisitLog tbIpVisitLog) {
        return baseMapper.insert(tbIpVisitLog);
    }

    /**
     * 根据条件更新tbIpVisitLog
     *
     * @param condition 新数据以及更新条件，使用tbIpVisitLog实体类封装
     * @return {@link int}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public int update(TbIpVisitLog condition) {
        //条件封装类
        UpdateWrapper<TbIpVisitLog> wrapper = new UpdateWrapper<TbIpVisitLog>();
//        wrapper.setEntity(condition);
        //封装更新条件，wrapper.eq(String column,Object value),column表示数据库字段，value是字段对应的值
            wrapper.setSql("`counts` = `counts` + 1")
                    .eq("ip",condition.getIp())
                    .eq("visit_date",condition.getVisitDate())
                    .eq("status", condition.getStatus());
        return baseMapper.update(null, wrapper);
    }

    /**
     * 根据条件删除tbIpVisitLog
     *
     * @param condition 删除条件，使用tbIpVisitLog实体类封装
     * @return {@link int}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public int delete(TbIpVisitLog condition) {
        //条件封装类
        UpdateWrapper<TbIpVisitLog> wrapper = new UpdateWrapper<TbIpVisitLog>();
        wrapper.setEntity(condition);
        //封装删除条件，wrapper.eq(String column,Object value),column表示数据库字段，value是字段对应的值

        return baseMapper.delete(wrapper);
    }

    /**
     * 根据id删除tbIpVisitLog
     *
     * @param id 删除条件，需要删除的id
     * @return {@link int}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public int deleteById(Serializable id) {
        return baseMapper.deleteById(id);
    }

    /**
     * 根据条件查询一条tbIpVisitLog
     *
     * @param condition 查询条件
     * @return {@link TbIpVisitLog}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public TbIpVisitLog selectOne(TbIpVisitLog condition) {
        QueryWrapper<TbIpVisitLog> wrapper = new QueryWrapper<>();
        wrapper.setEntity(condition);
        //封装查询条件，wrapper.eq(String column,Object value),column表示数据库字段，value是字段对应的值

        return baseMapper.selectOne(wrapper);
    }

    /**
     * 根据id查询tbIpVisitLog
     *
     * @param id id
     * @return {@link TbIpVisitLog}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public TbIpVisitLog selectById(Serializable id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据条件查询tbIpVisitLog
     *
     * @param condition 查询条件
     * @return {@link List<TbIpVisitLog>}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public List<TbIpVisitLog> selectList(TbIpVisitLog condition) {
        QueryWrapper<TbIpVisitLog> wrapper = new QueryWrapper<>();
        wrapper.setEntity(condition);
        //封装查询条件，wrapper.eq(String column,Object value),column表示数据库字段，value是字段对应的值

        return baseMapper.selectList(wrapper);
    }

    /**
     * 根据条件统计tbIpVisitLog
     *
     * @param condition 统计条件
     * @return {@link Integer}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public Integer selectCount(TbIpVisitLog condition) {
        QueryWrapper<TbIpVisitLog> wrapper = new QueryWrapper<>();
        wrapper.setEntity(condition);
        //封装查询条件，wrapper.eq(String column,Object value),column表示数据库字段，value是字段对应的值

        return baseMapper.selectCount(wrapper);
    }
}
